package com.itheima.dao;


import com.itheima.domain.Teacher;

public class TeacherDao {
    private static Teacher[] teachers = new Teacher[5];

    //添加学生的方法
    public boolean addTeacher(Teacher teacher) {
        for (int i = 0; i < teachers.length; i++) {
            if (teachers[i] == null) {
                teachers[i] = teacher;
                break;
            }
        }
        return true;
    }

    //删除学生的方法
    public void deleteTeacher(String sid) {

        int index = getIndex(sid);
        teachers[index] = null;
    }

    //修改学生的方法
    public void updateTeacher(String sid, Teacher teacher) {
        int index = getIndex(sid);
        teachers[index] = teacher;
    }

    //查看学生的方法
    public Teacher[] showTeacher() {
        return teachers;
    }

    //给Service返回数组的方法
    public Teacher[] findAllTeacher() {
        return teachers;
    }

    //根据学号查找索引的方法
    private int getIndex(String sid) {
        int index = -1;
        for (int i = 0; i < teachers.length; i++) {
            if (teachers[i] != null) {
                if (teachers[i].getSid().equals(sid)) {
                    index = i;
                }
            }
        }
        return index;
    }

}
